package com.lmaye.design.patterns.creation.singleton;

import java.util.Objects;

/**
 * -- 测试
 *
 * @author lmay.Zhou
 * @date 2020/11/13 15:16 星期五
 * @qq 379839355
 * @email lmay@lmaye.com
 * @since JDK1.8
 */
public class SingletonTest {
    public static void main(String[] args) {
//        singletonHungryTest();
//        singletonLazy01Test();
//        singletonLazy02Test();
//        singletonLazy03Test();
//        singletonDoubleCheckTest();
//        singletonStaticInnerTest();
        singletonEnumTest();
    }

    private static void singletonEnumTest() {
        // 枚举
        SingletonEnum instance1 = SingletonEnum.INSTANCE;
        SingletonEnum instance2 = SingletonEnum.INSTANCE;
        System.out.println(Objects.equals(instance1, instance2));
        System.out.println("HashCode: " + instance1.hashCode() + ", " + instance2.hashCode());
    }

    private static void singletonStaticInnerTest() {
        // 静态内部类
        SingletonStaticInner instance1 = SingletonStaticInner.getInstance();
        SingletonStaticInner instance2 = SingletonStaticInner.getInstance();
        System.out.println(Objects.equals(instance1, instance2));
        System.out.println("HashCode: " + instance1.hashCode() + ", " + instance2.hashCode());
    }

    private static void singletonDoubleCheckTest() {
        // 双重检查
        new Thread(() -> System.out.println(SingletonDoubleCheck.getInstance())).start();
        System.out.println(SingletonDoubleCheck.getInstance());
    }

    private static void singletonLazy03Test() {
        // 懒汉式-线程不安全，同步代码块
        // 多线程演示
        new Thread(() -> System.out.println(SingletonLazy03.getInstance())).start();
        System.out.println(SingletonLazy03.getInstance());
    }

    private static void singletonLazy02Test() {
        // 懒汉式-线程安全，同步方法
        SingletonLazy02 lazy011 = SingletonLazy02.getInstance();
        SingletonLazy02 lazy012 = SingletonLazy02.getInstance();
        System.out.println("---------> 饿汉式-线程安全，同步方法");
        System.out.println(Objects.equals(lazy011, lazy012));
        System.out.println("HashCode: " + lazy011.hashCode() + ", " + lazy012.hashCode());
        // 多线程演示
        /*new Thread(() -> System.out.println(SingletonLazy02.getInstance())).start();
        System.out.println(SingletonLazy02.getInstance());*/
    }

    private static void singletonLazy01Test() {
        // 懒汉式-线程不安全
        /*SingletonLazy01 lazy011 = SingletonLazy01.getInstance();
        SingletonLazy01 lazy012 = SingletonLazy01.getInstance();
        System.out.println("---------> 饿汉式-线程不安全");
        System.out.println(Objects.equals(lazy011, lazy012));
        System.out.println("HashCode: " + lazy011.hashCode() + ", " + lazy012.hashCode());*/
        // 多线程演示
        new Thread(() -> System.out.println(SingletonLazy01.getInstance())).start();
        System.out.println(SingletonLazy01.getInstance());
    }

    private static void singletonHungryTest() {
        // 饿汉式-静态常量
        SingletonHungry01 hungry011 = SingletonHungry01.getInstance();
        SingletonHungry01 hungry012 = SingletonHungry01.getInstance();
        System.out.println("---------> 饿汉式-静态常量");
        System.out.println(Objects.equals(hungry011, hungry012));
        System.out.println("HashCode: " + hungry011.hashCode() + ", " + hungry012.hashCode());
        // 饿汉式-静态代码块
        SingletonHungry02 hungry021 = SingletonHungry02.getInstance();
        SingletonHungry02 hungry022 = SingletonHungry02.getInstance();
        System.out.println("---------> 饿汉式-静态代码块");
        System.out.println(Objects.equals(hungry021, hungry022));
        System.out.println("HashCode: " + hungry021.hashCode() + ", " + hungry022.hashCode());
    }
}
